package org.geek4j.iTextT;

import java.io.FileOutputStream;

import org.junit.Test;

import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Image;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

/**
 * @author Geek4J
 * @description iTextPDF T
 */
public class CebservT {

	/**
	 * @author Geek4J
	 * @description Table T
	 */
	@Test
	public void tableT() throws Exception {
		Document document = new Document();

		PdfWriter.getInstance(document, new FileOutputStream("cebserv-report.pdf"));

		document.open();
		String fontPath = CebservT.class.getResource("simsun.ttc").getPath();
		BaseFont baseFont = BaseFont.createFont(fontPath + ",0", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
		Font contentFont = new Font(baseFont, 9, Font.BOLD);

		// add company info
		Paragraph companyInfo = new Paragraph();
		Font companyInfoFont = new Font(baseFont, 9, Font.BOLD);
		companyInfo.setIndentationLeft(310);
		// add empty line
		companyInfo.add(new Paragraph(" "));
		companyInfo.add(new Paragraph(" "));
		companyInfo.add(
				new Paragraph("北京神州光大科技有限公司\n北京市海淀区上地六街康得大厦三层6305A室\nwww.cebserv.com\n400-056-1166", companyInfoFont));
		companyInfo.add(new Paragraph(" "));
		companyInfo.add(new Paragraph(" "));
		companyInfo.add(new Paragraph(" "));

		document.add(companyInfo);

		// add logo
		String logoPath = CebservT.class.getResource("cebserv.png").getPath();
		Image logo = Image.getInstance(logoPath);
		logo.scaleAbsolute(170, 50);
		logo.setAbsolutePosition(60, 720);
		document.add(logo);

		// add title
		Font titleFont = new Font(baseFont, 20, Font.BOLD);
		Paragraph title = new Paragraph("参考报价单", titleFont);

		title.setAlignment(Element.ALIGN_CENTER);

		document.add(title);

		// add seperator
		Paragraph contentContainer = new Paragraph();

		PdfPTable separator = new PdfPTable(1);
		separator.setWidthPercentage(90);
		separator.getDefaultCell().setBorderWidthTop(0);
		separator.getDefaultCell().setBorderWidthLeft(0);
		separator.getDefaultCell().setBorderWidthBottom(2);
		separator.getDefaultCell().setBorderWidthRight(0);
		separator.addCell(" ");

		contentContainer.add(separator);
		document.add(contentContainer);

		// no and date container
		Paragraph noAndDateContainer = new Paragraph();

		// add no
		Paragraph noContainer = new Paragraph();
		noAndDateContainer.setIndentationLeft(50);
		Chunk noLabel = new Chunk("编号：", contentFont);

		Chunk noValue = new Chunk("Q-TSS-2016-07-04-001", contentFont);
		noValue.setUnderline(1.5F, -2f);

		noContainer.add(noLabel);
		noContainer.add(noValue);
		noAndDateContainer.add(noContainer);

		// add date
		Paragraph dateContainer = new Paragraph();
		Chunk dateLabel = new Chunk("日期：", contentFont);

		Chunk dateValue = new Chunk("2016-07-04", contentFont);
		dateValue.setUnderline(1.5F, -2f);

		dateContainer.add(dateLabel);
		dateContainer.add(dateValue);
		noAndDateContainer.add(dateContainer);

		// add empty line
		noAndDateContainer.add(new Paragraph(" "));

		document.add(noAndDateContainer);

		// customer container

		// add customer info
		PdfPTable customerInfoTable = new PdfPTable(new float[] { 1, 3 });
		customerInfoTable.setWidthPercentage(65);

		PdfPCell customerNameLabel = new PdfPCell(new Paragraph("致", contentFont));
		customerNameLabel.setBorderWidth(1);
		customerNameLabel.setHorizontalAlignment(Element.ALIGN_RIGHT);
		customerNameLabel.setVerticalAlignment(Element.ALIGN_MIDDLE);
		customerInfoTable.addCell(customerNameLabel);
		PdfPCell customerNameValue = new PdfPCell(new Paragraph("宋骁", contentFont));
		customerNameValue.setBorderWidth(1);
		customerNameValue.setHorizontalAlignment(Element.ALIGN_CENTER);
		customerNameValue.setVerticalAlignment(Element.ALIGN_MIDDLE);
		customerInfoTable.addCell(customerNameValue);

		PdfPCell companyNameLabel = new PdfPCell(new Paragraph("公司", contentFont));
		companyNameLabel.setHorizontalAlignment(Element.ALIGN_RIGHT);
		companyNameLabel.setVerticalAlignment(Element.ALIGN_MIDDLE);
		companyNameLabel.setBorderWidth(1);
		customerInfoTable.addCell(companyNameLabel);
		PdfPCell companyNameValue = new PdfPCell(new Paragraph("文思海辉", contentFont));
		companyNameValue.setHorizontalAlignment(Element.ALIGN_CENTER);
		companyNameValue.setVerticalAlignment(Element.ALIGN_MIDDLE);
		companyNameValue.setBorderWidth(1);
		customerInfoTable.addCell(companyNameValue);

		PdfPCell companyAddressLabel = new PdfPCell(new Paragraph("地址", contentFont));
		companyAddressLabel.setHorizontalAlignment(Element.ALIGN_RIGHT);
		companyAddressLabel.setVerticalAlignment(Element.ALIGN_MIDDLE);
		companyAddressLabel.setBorderWidth(1);
		customerInfoTable.addCell(companyAddressLabel);
		PdfPCell companyAddressValue = new PdfPCell(new Paragraph("北京 市 海淀 区 上地七街 康德大厦", contentFont));
		companyAddressValue.setHorizontalAlignment(Element.ALIGN_CENTER);
		companyAddressValue.setVerticalAlignment(Element.ALIGN_MIDDLE);
		companyAddressValue.setBorderWidth(1);
		customerInfoTable.addCell(companyAddressValue);

		PdfPCell companyPhoneLabel = new PdfPCell(new Paragraph("电话", contentFont));
		companyPhoneLabel.setHorizontalAlignment(Element.ALIGN_RIGHT);
		companyPhoneLabel.setVerticalAlignment(Element.ALIGN_MIDDLE);
		companyPhoneLabel.setBorderWidth(1);
		customerInfoTable.addCell(companyPhoneLabel);
		PdfPCell companyPhoneValue = new PdfPCell(new Paragraph("13164444400", contentFont));
		companyPhoneValue.setHorizontalAlignment(Element.ALIGN_CENTER);
		companyPhoneValue.setVerticalAlignment(Element.ALIGN_MIDDLE);
		companyPhoneValue.setBorderWidth(1);
		customerInfoTable.addCell(companyPhoneValue);

		PdfPCell companyEmailLabel = new PdfPCell(new Paragraph("邮箱", contentFont));
		companyEmailLabel.setHorizontalAlignment(Element.ALIGN_RIGHT);
		companyEmailLabel.setVerticalAlignment(Element.ALIGN_MIDDLE);
		companyEmailLabel.setBorderWidth(1);
		customerInfoTable.addCell(companyEmailLabel);
		PdfPCell companyEmailValue = new PdfPCell(new Paragraph("Geek4J@GMAIL.COM", contentFont));
		companyEmailValue.setHorizontalAlignment(Element.ALIGN_CENTER);
		companyEmailValue.setVerticalAlignment(Element.ALIGN_MIDDLE);
		companyEmailValue.setBorderWidth(1);
		customerInfoTable.addCell(companyEmailValue);

		document.add(customerInfoTable);
		// add empty line
		document.add(new Paragraph(" "));

		// add table header
		PdfPTable header = new PdfPTable(7);
		header.setWidthPercentage(90);
		PdfPCell itemHeader = new PdfPCell(new Paragraph("条目", contentFont));
		itemHeader.setFixedHeight(20);
		itemHeader.setBorderWidth(1);
		itemHeader.setHorizontalAlignment(Element.ALIGN_CENTER);
		itemHeader.setVerticalAlignment(Element.ALIGN_MIDDLE);

		PdfPCell supplierHeader = new PdfPCell(new Paragraph("厂商", contentFont));
		supplierHeader.setBorderWidth(1);
		supplierHeader.setHorizontalAlignment(Element.ALIGN_CENTER);
		supplierHeader.setVerticalAlignment(Element.ALIGN_MIDDLE);

		PdfPCell categoryHeader = new PdfPCell(new Paragraph("类型", contentFont));
		categoryHeader.setBorderWidth(1);
		categoryHeader.setHorizontalAlignment(Element.ALIGN_CENTER);
		categoryHeader.setVerticalAlignment(Element.ALIGN_MIDDLE);

		PdfPCell seriesHeader = new PdfPCell(new Paragraph("系列", contentFont));
		seriesHeader.setBorderWidth(1);
		seriesHeader.setHorizontalAlignment(Element.ALIGN_CENTER);
		seriesHeader.setVerticalAlignment(Element.ALIGN_MIDDLE);

		PdfPCell moduleHeader = new PdfPCell(new Paragraph("功能模块", contentFont));
		moduleHeader.setBorderWidth(1);
		moduleHeader.setHorizontalAlignment(Element.ALIGN_CENTER);
		moduleHeader.setVerticalAlignment(Element.ALIGN_MIDDLE);

		PdfPCell amountHeader = new PdfPCell(new Paragraph("数量", contentFont));
		amountHeader.setBorderWidth(1);
		amountHeader.setHorizontalAlignment(Element.ALIGN_CENTER);
		amountHeader.setVerticalAlignment(Element.ALIGN_MIDDLE);

		PdfPCell priceHeader = new PdfPCell(new Paragraph("参考报价", contentFont));
		priceHeader.setBorderWidth(1);
		priceHeader.setHorizontalAlignment(Element.ALIGN_CENTER);
		priceHeader.setVerticalAlignment(Element.ALIGN_MIDDLE);

		header.addCell(itemHeader);
		header.addCell(supplierHeader);
		header.addCell(categoryHeader);
		header.addCell(seriesHeader);
		header.addCell(moduleHeader);
		header.addCell(amountHeader);
		header.addCell(priceHeader);

		document.add(header);

		// add table service
		PdfPTable service = new PdfPTable(7);
		service.setWidthPercentage(90);
		PdfPCell service0 = new PdfPCell(new Paragraph("服务：北京 市 海淀 区 上地七街", contentFont));
		service0.setFixedHeight(22);
		service0.setColspan(7);
		service0.setBorderWidth(1);
		service0.setVerticalAlignment(Element.ALIGN_MIDDLE);
		service.addCell(service0);

		document.add(service);

		// add table content
		for (int i = 0; i < 7; i++) {
			PdfPTable content = new PdfPTable(7);
			content.setWidthPercentage(90);
			for (int j = 0; j < 7; j++) {
				PdfPCell column = new PdfPCell(
						new Paragraph("Data {0} * {1}".replace("{0}", i + "").replace("{1}", j + ""), contentFont));
				column.setFixedHeight(20);
				column.setBorderWidth(1);
				column.setHorizontalAlignment(Element.ALIGN_CENTER);
				column.setVerticalAlignment(Element.ALIGN_MIDDLE);
				content.addCell(column);
			}
			document.add(content);
		}

		// add table service content
		PdfPTable serviceContentContainer = new PdfPTable(1);
		serviceContentContainer.setWidthPercentage(90);

		PdfPCell serviceContentContainerCell = new PdfPCell();
		serviceContentContainerCell.setBorderWidth(1);

		PdfPTable serviceContent = new PdfPTable(new float[] { 1, 5 });
		serviceContent.setWidthPercentage(100);

		PdfPCell serviceContentLabel = new PdfPCell(new Paragraph("服务内容包括：", contentFont));
		serviceContentLabel.setFixedHeight(33);
		serviceContentLabel.setBorderWidth(0);
		serviceContentLabel.setVerticalAlignment(Element.ALIGN_MIDDLE);
		serviceContent.addCell(serviceContentLabel);

		PdfPCell serviceContentValue = new PdfPCell(new Paragraph(
				"设备维修服务所覆盖的IT设备类别包括网络设备、服务器设备和存储设备，设备型号基本覆盖了市场上主流的网络、服务器和存储设备品牌，我们郑重承诺针对所有已维修设备免费费提供180天质保服务。",
				contentFont));
		serviceContentValue.setFixedHeight(33);
		serviceContentValue.setBorderWidth(0);
		serviceContentValue.setVerticalAlignment(Element.ALIGN_MIDDLE);
		serviceContent.addCell(serviceContentValue);

		serviceContentContainerCell.addElement(serviceContent);
		serviceContentContainer.addCell(serviceContentContainerCell);
		document.add(serviceContentContainer);

		// add table service content exclude
		PdfPTable serviceContentExcludeContainer = new PdfPTable(1);
		serviceContentExcludeContainer.setWidthPercentage(90);
		PdfPCell serviceContentExcludeContainerCell = new PdfPCell();
		serviceContentExcludeContainerCell.setBorderWidth(1);

		PdfPTable serviceContentExclude = new PdfPTable(new float[] { 1, 5 });
		serviceContentExclude.setWidthPercentage(100);
		PdfPCell serviceContentExcludeLabel = new PdfPCell(new Paragraph("服务内容不包括：", contentFont));
		serviceContentExcludeLabel.setFixedHeight(33);
		serviceContentExcludeLabel.setBorderWidth(0);
		serviceContentExcludeLabel.setVerticalAlignment(Element.ALIGN_MIDDLE);
		serviceContentExclude.addCell(serviceContentExcludeLabel);

		PdfPCell serviceContentExcludeValue = new PdfPCell(new Paragraph(
				"设备维修服务所覆盖的IT设备类别包括网络设备、服务器设备和存储设备，设备型号基本覆盖了市场上主流的网络、服务器和存储设备品牌，我们郑重承诺针对所有已维修设备免费费提供180天质保服务。",
				contentFont));
		serviceContentExcludeValue.setFixedHeight(33);
		serviceContentExcludeValue.setBorderWidth(0);
		serviceContentExcludeValue.setVerticalAlignment(Element.ALIGN_MIDDLE);
		serviceContentExclude.addCell(serviceContentExcludeValue);

		serviceContentExcludeContainerCell.addElement(serviceContentExclude);
		serviceContentExcludeContainer.addCell(serviceContentExcludeContainerCell);
		document.add(serviceContentExcludeContainer);

		// add total
		Paragraph totalContainer = new Paragraph();
		totalContainer.setIndentationLeft(370);
		Chunk totalLabel = new Chunk("参考价格总计：", contentFont);

		Chunk totalValue = new Chunk("Q-TSS-2016-07-04-001", contentFont);
		totalValue.setUnderline(1.5F, -2f);

		totalContainer.add(totalLabel);
		totalContainer.add(totalValue);

		document.add(totalContainer);

		// add memo
		Paragraph memo = new Paragraph();
		Font memoFont = new Font(baseFont, 9, Font.BOLD);
		memo.setIndentationLeft(30);
		// add empty line
		memo.add(new Paragraph(" "));
		memo.add(new Paragraph(" "));
		memo.add(new Paragraph(
				"备注:\n1,工程师类服务低于600元的按照600元计算,即工程师服务最低报价为600元\n2,以上报价仅供参考,以平台服务人员给出的正式报价为准\n3,如果您有任何疑问,请拨打400-056-1166",
				memoFont));

		document.add(memo);

		document.close();
	}
}